﻿/*----------------------------------------------------------------
// Copyright (C) 2014 苏州麦迪斯顿医疗科技股份有限公司
// 文件名：ConnectionMonitor.cs
// 文件功能描述：ConnectionMonitor
// 创建标识：顾伟伟-2014-04-03
----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;

namespace CPOE.Framework
{
    /// <summary>
    /// 客户端网络连接监视器,当客户端网络连接发生变化的时候，测试本地网络连接是否建立，能否访问远程服务器。
    /// </summary>
    public class ConnectionMonitor : IDisposable
    {
        /// <summary>
        /// 网络状态改变通知事件
        /// </summary>
        public event EventHandler<StateChangedEventArgs> StateChangedNotification;
        /// <summary>
        /// 连接器集合
        /// </summary>
        private List<Connection> _connections = null;
        /// <summary>
        /// 监视器是否已释放并关闭
        /// </summary>
        private bool _isDisposed = false;

        internal ConnectionMonitor(List<Connection> connections)
        {
            _connections = connections;

            foreach (var connection in _connections)
            {
                connection.StateChanged += connection_StateChanged;
            }

            //注册操作系统的网络可用性更改事件
            NetworkChange.NetworkAvailabilityChanged += Network_ChangedHandler;
        }
        /// <summary>
        /// 本地网络参数改变事件处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private  void Network_ChangedHandler(object sender, EventArgs e)
        {
            Update();
        }
        /// <summary>
        /// 连接状态改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private  void connection_StateChanged(object sender, StateChangedEventArgs e)
        {
            //向事件定阅方发出事件通知
            if (StateChangedNotification != null)
                StateChangedNotification(sender, e);
        }
        /// <summary>
        /// 更新网络和服务器连接状态,超时长默认为5秒钟
        /// </summary>
        public void Update()
        {
            Update(5000); 
        }
        /// <summary>
        /// 根据传入的超时时长值，更新网络和服务器连接状态
        /// </summary>
        /// <param name="timeout">超时长长(毫秒)</param>
        public void Update(int timeout)
        {
            if (_isDisposed)
                throw new InvalidOperationException("连接监视器已经释放并关闭");

            foreach (Connection connection in _connections)
            {
                connection.UpdateStatus(timeout);
            }
        }
        /// <summary>
        /// 释放资源，关闭监视器
        /// </summary>
        public void Dispose()
        {
            if (_connections != null)
            {
                foreach (var connection in _connections)
                {
                    connection.StateChanged -= connection_StateChanged;
                    connection.Dispose();
                }
            }

            NetworkChange.NetworkAvailabilityChanged -= Network_ChangedHandler;

            _isDisposed = true;
        }
    }
}
